//
//  NetWorkTools+home.swift
//  FM
//
//  Created by x on 2017/10/31.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

extension NetWorTools {
    
    /// 加载首页显示数据
    func loadHomeData(successCallBack:@escaping (_ dataSourc:[AnyObject])->(), errorCallback:@escaping (_ error: Error)->()) -> Void {
        
        let url = "http://yiapi.xinli001.com/fm/home-list.json"
        let param = ["key":"c0d28ec0954084b4426223366293d190"]
        
        get(url, parameters: param, progress: { (_) in
            
        }, success: { (_, JSON) in
            //print(JSON ?? 99)
            let data = (JSON as! [String:AnyObject])["data"]
            var allData:[AnyObject] = [AnyObject]()
            //1.拿到推荐的数据
            allData.append((RecomomendModel.analysisData(data: data)) as AnyObject)
            
            //2.拿到分类
            allData.append((CategoryModel.analysisData(data: data)) as AnyObject)
            
            //4精彩推荐的数据
            let hotfm = data?["hotfm"] as! [[String:AnyObject]]
            allData.append((HomeFM.analysisData(datas: hotfm)) as AnyObject)
            
            //5新FM的数据
            let newfm = data?["newfm"] as! [[String:AnyObject]]
            allData.append((HomeFM.analysisData(datas: newfm)) as AnyObject)
            
            //6最新心理科的数据newlesson
            let newlesson = data?["newlesson"] as! [[String:AnyObject]]
            allData.append((HomeFM.analysisData(datas: newlesson)) as AnyObject)
            
            //7最新电台数据
            let diantai = data?["diantai"] as! [[String: AnyObject]]
            allData.append((Radio.analysisData(datas: diantai)) as AnyObject)
            
            //8.通知home
            successCallBack(allData)
        }) { (_, error) in
            errorCallback(error)
        }
    }
    
    /// 加载首页分类数据
    ///
    /// - Parameters:
    ///   - categoryID: 分类的ID
    ///   - offset: 相对上次偏移多少条数据
    func loadCategoryData(categoryID:String, offset:Int, successCallBack:@escaping (_ dataSource:[HomeFM])->(), errorCallBack:@escaping (_ error:Error)->()) -> Void {
        let url = "http://yiapi.xinli001.com/fm/category-jiemu-list.json"
        let param = ["key":"c0d28ec0954084b4426223366293d190","category_id":categoryID,"limit":20,"offset":offset] as [String : Any]
        
        get(url, parameters: param, progress: nil, success: { (task, JSON) in
            //print(task.currentRequest?.url?.description)
            var categorys:[HomeFM] = [HomeFM]()
            let data = ((JSON as! [String:AnyObject])["data"]) as! [[String:AnyObject]]
            for dic in data {
                categorys.append(HomeFM(dict: dic))
            }
            
            successCallBack(categorys)
        }) { (_, error) in
            errorCallBack(error)
        }
    }
    
    /// 加载首页分类中fm的详细信息(评论)
    ///
    /// - Parameters:
    ///   - broadcast_id: fm的ID
    ///   - offset: 从第几条开始加载
    ///   - successCallBack: <#successCallBack description#>
    ///   - errorCallBack: <#errorCallBack description#>
    func loadFMDetailsInfoData(broadcast_id:String, offset:Int, successCallBack:@escaping (_ dataSource:[Comment])->(), errorCallBack:@escaping (_ error:Error)->()) -> Void {
        //http://yiapi.xinli001.com/fm/comment-list.json?broadcast_id=99390917&key=c0d28ec0954084b4426223366293d190&offset=0&limit=10
        let url = "http://yiapi.xinli001.com/fm/comment-list.json"
        let param = ["broadcast_id":broadcast_id,"key":"c0d28ec0954084b4426223366293d190","offset":offset,"limit":10] as [String:Any]
        get(url, parameters: param, progress: nil, success: { (task, JSON) in
            //print(task.currentRequest?.url?.description)
            let data = (JSON as! [String:AnyObject])["data"] as! [[String:AnyObject]]
            var models:[Comment] = [Comment]()
            for dic in data {
                models.append(Comment(dic: dic))
            }
            successCallBack(models)
        }) { (_, error) in
            errorCallBack(error)
        }
    }
    
    
//    更多FM
//    http://bapi.xinli001.com/fm2/broadcast_list.json/?is_teacher=0&key=c0d28ec0954084b4426223366293d190&rows=15&offset=0
    
//    更多心理科
//    http://bapi.xinli001.com/fm2/broadcast_list.json/?is_teacher=1&key=c0d28ec0954084b4426223366293d190&rows=15&offset=0

    /// 加载更多FM和心理科
    ///
    /// - Parameters:
    ///   - offset: <#offset description#>
    ///   - tag: <#tag description#>
    ///   - finished: <#finished description#>
    func loadNewFMorPsychology(is_teacher:Int, offset:Int, finished:@escaping (_ data:[HomeFM]?, _ error:Error?)->()) -> Void{
        let url = "http://bapi.xinli001.com/fm2/broadcast_list.json/"
        let param = ["key":"c0d28ec0954084b4426223366293d190","is_teacher":is_teacher,"rows":15,"offset":offset] as [String:Any]
    
        get(url, parameters: param, progress: nil, success: { (_, JSON) in
            let data = (JSON as! [String:AnyObject])["data"] as! [[String:AnyObject]]
            var models:[HomeFM] = [HomeFM]()
            for dic in data {
                models.append(HomeFM(dict: dic))
            }
            finished(models, nil)
        }) { (_, error) in
            finished(nil, error)
        }
    }
    
    
    //http://yiapi.xinli001.com/fm/diantai-jiemu-list.json?key=c0d28ec0954084b4426223366293d190&diantai_id=905&offset=20&limit=20
    /// 加在首页电台详情
    ///
    /// - Parameters:
    ///   - diantai_id: <#diantai_id description#>
    ///   - offset: <#offset description#>
    ///   - finished: <#finished description#>
    func loadRadioDetails(diantai_id:String, offset:Int, finished:@escaping (_ data:[HomeFM]?, _ error:Error?)->()) -> Void {
        let url = "http://yiapi.xinli001.com/fm/diantai-jiemu-list.json"
        let param = ["diantai_id":diantai_id,"key":"c0d28ec0954084b4426223366293d190","offset":offset,"limit":20] as [String:Any]
        
        get(url, parameters: param, progress: nil, success: { (_, JSON) in
            let data = (JSON as! [String:AnyObject])["data"] as! [[String:AnyObject]]
            var models:[HomeFM] = [HomeFM]()
            for dic in data {
                models.append(HomeFM(dict: dic))
            }
            finished(models, nil)
            
        }) { (_, error) in
            finished(nil, error)
        }
    }
    
    //http://yiapi.xinli001.com/fm/diantai-page.json?key=c0d28ec0954084b4426223366293d190
    
    /// 加载更多电台
    ///
    /// - Parameter finished: <#finished description#>
    func loadMoreRadio(finished:@escaping (_ data:[[Radio]]?, _ error:Error?)->()) -> Void {
        let url  = "http://yiapi.xinli001.com/fm/diantai-page.json"
        let param = ["key":"c0d28ec0954084b4426223366293d190"]
        
        get(url, parameters: param, progress: nil, success: { (_, JSON) in
            let data = (JSON as! [String:AnyObject])["data"] as! [String:AnyObject]
            var allData:[[Radio]] = [[Radio]]()
            //1.取出数据
            allData.append(Radio.analysisData(datas: data["tuijian"] as! [[String : AnyObject]]))
            allData.append(Radio.analysisData(datas: data["newlist"] as! [[String : AnyObject]]))
            allData.append(Radio.analysisData(datas: data["hotlist"] as! [[String : AnyObject]]))
            
            finished(allData, nil)
            
        }) { (_, error) in
            finished(nil, error)
        }
    }
    
    
    //http://yiapi.xinli001.com/fm/diantai-new-list.json?key=c0d28ec0954084b4426223366293d190&limit=20&offset=0
    
    /// 加载更多主播
    ///
    /// - Parameters:
    ///   - offset: <#offset description#>
    ///   - finished: <#finished description#>
    func loadMoreAnchor(offset:Int, finished:@escaping (_ data:[Radio]?, _ error:Error?)->()) -> Void {
        let url  = "http://yiapi.xinli001.com/fm/diantai-new-list.json"
        let param = ["key":"c0d28ec0954084b4426223366293d190","offset":offset,"limit":20] as [String:Any]
        
        get(url, parameters: param, progress: nil, success: { (_, JSON) in
            let data = (JSON as! [String:AnyObject])["data"] as! [[String:AnyObject]]
            var radios:[Radio] = [Radio]()
            
            for dic in data {
                radios.append(Radio(dic: dic))
            }
            finished(radios, nil)
            
        }) { (_, error) in
            finished(nil, error)
        }
    }
}










